using System;
using System.Collections.Generic;
using System.Text;
using SharpMap.Presentation.Presenters;
using SharpMap.Presentation.Views;
using SharpMap.Layers;
using SharpMap.Wpf.Rendering;
using System.ComponentModel;
using SharpMap.Styles;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
namespace SharpMap.Presentation.Wpf
{

    /// <summary>
    /// Handles inputs from <see cref="WpfView>"/> and updates the corresponding layer.
    /// </summary>
    class LayerStyleViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<INotifyPropertyChanged> StyleList
        {
            get { return _styleList; }
            set { _styleList = value; }
        }

        public LayerStyleViewModel(ILayer layer, FrameworkElement view)
        {
            _layer = layer;
            _style = layer.Style;
            layer.PropertyChanged += new PropertyChangedEventHandler(LayerStyleViewModel_PropertyChanged);
            VectorStyle vs = layer.Style as VectorStyle;
            if (vs != null)
            {
                StyleList = StyleViewModelFactory.GetStylePropertyList(vs);

                ICollectionView cview = CollectionViewSource.GetDefaultView(StyleList);
                cview.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
            }
        }

 
        void LayerStyleViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            string name = e.PropertyName;


        }

        public void ViewLayerStyleChangeRequested(object sender, LayerStyleChangeEventArgs e)
        {
            _layer.Style = e.Style;
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;


        protected void RaisePropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                PropertyChangedEventArgs args =
                    new PropertyChangedEventArgs(propertyName);
                handler(this, args);
            }
        }


        #endregion

        ILayer _layer;
        IStyle _style;
        ObservableCollection<INotifyPropertyChanged> _styleList = new ObservableCollection<INotifyPropertyChanged>();
    }

    public static class StyleCloner
    {
        public static IStyle Clone(this IStyle style)
       {
            VectorStyle vs = style as VectorStyle;
            if(vs != null)
            {
                VectorStyle v = new VectorStyle();
                v.AreFeaturesSelectable = vs.AreFeaturesSelectable;
                v.Enabled = vs.Enabled;
                v.EnableOutline = vs.EnableOutline;
                v.Fill = vs.Fill;
                v.HighlightFill = vs.HighlightFill;
                v.HighlightLine = vs.HighlightLine;
                v.HighlightOutline = vs.HighlightOutline;
                v.HighlightSymbol = vs.HighlightSymbol;
                v.Line = vs.Line;
                v.MaxVisible = vs.MaxVisible;
                v.MinVisible = vs.MinVisible;
                v.Outline = vs.Outline;
                v.RenderingMode = vs.RenderingMode;
                v.SelectFill = vs.SelectFill;
                v.SelectLine = vs.SelectLine;
                v.SelectOutline = vs.SelectOutline;
                v.SelectSymbol = vs.SelectSymbol;
                v.Symbol = vs.Symbol;
                v.TextRenderingHint = vs.TextRenderingHint;
               
                 return v;
            }
            return null;
       }
    }

    public sealed class LayerStyleChangeEventArgs : EventArgs
    {
        private readonly IStyle _style;
        StyleViewEntry _viewEntry;

        public StyleViewEntry ViewEntry
        {
            get { return _viewEntry; }
        }

        public LayerStyleChangeEventArgs(IStyle style, StyleViewEntry entry)
        {
            _style = style.Clone();
            
            _viewEntry = entry;
            entry.UpdateStyle(_style);
        }

        public IStyle Style
        {
            get { return _style; }
        }
    }

}
